#include <cstring>
#include <iostream>
using namespace std;

const unsigned int blocksize = 8;

/**
 * @brief 以8字节为单位进行亦或加解密
 *
 * @param data 输入数据
 * @param datasize 输入数据长
 * @param outdata 输出数据
 * @param outdatalen 输出数据缓冲区长度
 * @param key 密钥
 *
 * @return 加密后数据长度
 */
int XorCipher64(unsigned char* data, unsigned int datasize, unsigned char* outdata, unsigned int outdatalen, unsigned long long key)
{
	const unsigned long long* temp = reinterpret_cast<const unsigned long long*>(data);
	unsigned long long* outtemp = reinterpret_cast<unsigned long long*>(outdata);
	unsigned int i = 0;
	for (; i < (datasize / blocksize); ++i) {
		outtemp[i] = temp[i] ^ key;
	}
	unsigned long long temppic = 0;
	void* o = reinterpret_cast<void*>(data + (datasize - (datasize % 8)));
	memcpy(&temppic, o, (datasize % 8));
	temppic = temppic ^ key;
	outtemp[i] = temppic;
	return 0;
}

int main()
{
	unsigned char p[] = "123456789";
	unsigned char q[16];
	XorCipher64(p, 9, q, 16, 12);
	for (int i = 0; i < 16; ++i)
		cout << q[i] << endl;
	unsigned char w[16];
	XorCipher64(q, 16, w, 16, 12);
	for (int i = 0; i < 16; ++i)
		cout << w[i] << endl;
}
